package com.yazi.mxz.server.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import javax.annotation.Resource;
import com.fasterxml.jackson.annotation.JsonView;
import com.yazi.mxz.common.entity.beans.Member;
import com.yazi.mxz.common.entity.beans.SecurityQuestion;
import com.yazi.mxz.common.service.MemberService;
import com.yazi.mxz.common.service.SecurityQuestionService;
import com.yazi.mxz.server.request.AddSecurityQuestionDoRequest;
import com.yazi.mxz.server.response.SecurityQuestionListResponse;
import com.yazi.mxz.server.response.StatusResponse;
import com.yazi.mxz.server.response.view.BaseView;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


@Controller("securityQuestionController")
@RequestMapping("/securityQuestion")
public class SecurityQuestionController extends BaseController {
	private static Logger log = LoggerFactory.getLogger(SecurityQuestionController.class);
	@Resource
	MemberService memberService;
	@Resource
	SecurityQuestionService securityQuestionService;
	
	@RequestMapping("/list")
	@ResponseBody
	@JsonView(BaseView.class)
	public SecurityQuestionListResponse list() {
		Member loginMember = getLoginMember();
		Long memberId = loginMember.getMemberId();
		log.debug("客户端 -> 获取用户安全问题列表：memberId={}", memberId);
		SecurityQuestionListResponse res = new SecurityQuestionListResponse();
		try {
			Set<SecurityQuestion> sets = loginMember.getSecurityQuestions();
			List<SecurityQuestion> list = new ArrayList<SecurityQuestion>();
			list.addAll(sets);
			res.setSuccessData(list);
			res.setOk();
		} catch (Exception e) {
			log.error("获取用户安全问题列表异常:", e);
			res.setErrorMessage("操作失败,请联系管理员");
		}

		return res;
	}
	
	@RequestMapping("/addDo")
	@ResponseBody
	public StatusResponse addDo(@RequestBody AddSecurityQuestionDoRequest req) {
		Member loginMember = getLoginMember();
		Long memberId = loginMember.getMemberId();
		log.debug("客户端 -> 添加安全问题：memberId={}", memberId);
		StatusResponse res = new StatusResponse();
		try {
			securityQuestionService.add(loginMember, req.getSecurityQuestions());
			res.setOk();
		} catch (Exception e) {
			log.error("客户端 -> 添加安全问题异常:", e);
			res.setErrorMessage("操作失败,请联系管理员");
		}
		return res;
	}
}
